//package com.elasticsearch.demo.config;
//
//import org.apache.http.HttpHost;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.stereotype.Component;
//
//import java.util.Arrays;
//import java.util.Objects;
//
//@Component
//public class EsRestClient {
//    @Value("${elasticsearch.rest.hosts}")//读取es主机+端口配置
//    private String hosts;
//
//
//    @Bean
//    public RestHighLevelClient initSimpleClient() {
//        //根据配置文件配置HttpHost数组
//        HttpHost[] httpHosts = Arrays.stream(hosts.split(",")).map(
//                host -> {
//                    //分隔es服务器IP和端口
//                    String[] hostParts = host.split(":");
//                    String hostName = hostParts[0];
//                    int port = Integer.parseInt(hostParts[1]);
//                    return new HttpHost(hostName, port, HttpHost.DEFAULT_SCHEME_NAME);
//                }).filter(Objects::nonNull).toArray(HttpHost[]::new);
//        //构建客户端
//        return new RestHighLevelClient(RestClient.builder(httpHosts));
//
//    }
//}
